import React, { Component } from 'react';
import {
  StyleSheet,
  View,
  Text,
  Image,
  ActivityIndicator,
  AsyncStorage,
  TouchableOpacity,
  Linking,
  ToastAndroid,
  TextInput,
  Button,
} from 'react-native';




export default class Login extends Component{
    static navigationOptions = {
        title: '账号密码登录',
        headerTitleStyle: {
            alignSelf:'center'
          },
          headerRight: (
      <Text></Text>
            ),
      };
     getuserquanxian=function(){
	                rolenum=0
	            //0 老板 1下单员2退单员3下单员和退单员
						roleinfo=global.userdata
						for(var i=0;i<roleinfo.roleList.length;i++){
							if(roleinfo.roleList[i].name=="老板"){
								rolenum=0;
								break
							}
							else if(roleinfo.roleList[i].name=="下单员"){
								global.roletype=1;
								rolenum++;
							}
							else if(roleinfo.roleList[i].name=="退单员"){
								global.roletype=2;
								rolenum++
							}
						}
						if(rolenum==0){
							global.roletype=0
						}
						else if(rolenum==2){
							global.roletype=3
						}
            }

      state = {
        loginName:'',
        password:''
      };


       login= async ()=>{ 
            try {
            let response = await fetch(global.mainurl+"bossHelper/login?loginName="+this.state.loginName+"&password="+this.state.password);
            var data=await response.json()
                    if(data.code==1){
                        console.log(data)
                        global.sellerid=data.data.user.office.id;
                        global.token=data.data.user.token
                        global.userdata=data.data.user
                        this.getuserquanxian()
                        //this.props.navigation.navigate("MyTab");
                        ToastAndroid.show('登陆成功', ToastAndroid.SHORT);
                    }
                    else if(data.code==0){
								ToastAndroid.show('账号或密码错误', ToastAndroid.SHORT);
							}
         
            } catch(error) {
            ToastAndroid.show('网络出错', ToastAndroid.SHORT);
            }
        


          
      }
      _submit= ()=>{
          this.setState(
              {
                  codeSent:false,
              }
          )
      }



      render(){
          return(
            <View style={styles.container}>
            <View style={styles.signupBox}>
             {
                 <View>
                 <TextInput
                 placeholder="输入账号"
                 autoCapitalize={'none'}
                 autoCorrect={false}
                 autoFocus={false}
                 underlineColorAndroid='transparent'
                 editable={true}
                 multiline={false}
                 keyboardType={'number-pad'}
                 style={styles.inputField}
                 onChangeText={(text)=>{
                     this.setState({
                         loginName:text
                     })
                 }}
                 />
                 <TextInput
                 placeholder="输入账号"
                 autoCapitalize={'none'}
                 autoCorrect={false}
                 autoFocus={false}
                 underlineColorAndroid='transparent'
                 editable={true}
                 multiline={false}
                 keyboardType={'number-pad'}
                 style={styles.inputField}
                 onChangeText={(text)=>{
                     this.setState({
                         password:text
                     })
                 }}
                 />
                 </View>
             }  

                 {
                     
                     
                     <Button
                       style={styles.btn}
                       onPress={()=>{this.login()}} title="立即登录"/>
                 }
            </View>
            <View style={styles.otherbox}>
            <View style={styles.other1}>
                <Image source={require('../img/poster.jpg')} style={styles.img}></Image>
            <Button onPress={
                ()=>{
                    this.props.navigation.navigate("Login")
                }
            }
             title="短信验证码登录" style={styles.other}/>
            </View>
            </View>
            </View>
          )
      }
}


var styles=StyleSheet.create({
    container:{
        flex:1,
        padding:10,
        backgroundColor:'#f9f9f9'
    },

    signupBox:{
        marginTop:10
    },

    title:{
        marginBottom:20,
        color:'#333',
        fontSize:20,
        textAlign:'center'
    },
    inputField:{
        height:40,
        padding:5,
        color:"#666",
        fontSize:16,
        borderRadius:4,
        backgroundColor:'white',
        marginBottom:10
    },

    btn:{
        padding:40,
        backgroundColor:'transparent',
        borderColor:'#ee735c',
        borderWidth:1,
        borderRadius:4,
        color:'#ee735c'
    },
    other:{
        marginTop:50,
        textAlign:'center'
    },
    other1:{
        flexDirection:'column',
        alignItems:'center',
    },
    otherbox:{
        marginTop:50,
        height:200,
        flexDirection:'column',
        alignItems:'center',
        justifyContent:'space-between'
    },
    img:{
        width:50,
        height:50,
        borderRadius:50,
        marginBottom:20
    }
})